package org.example.Mapper;

import org.apache.ibatis.annotations.*;
import org.example.domain.User;

import java.util.Date;

@Mapper
public interface UserMapper {

    @Select("SELECT * FROM user WHERE account=#{account};")
    User login(String account);

    @Select("SELECT * FROM user WHERE id=#{id} LIMIT 1;")
    User getAllById(Integer id);

    @Select("SELECT * FROM user WHERE p_st!=0 OR p_st_szpg!=0 OR p_st_xkqz!=0 OR p_st_tyqp!=0 OR p_st_myrx!=0 OR " +
            "p_st_ldqj!=0 OR p_st_cxcy!=0;")
    User[] getUserBySt();//获取所有素拓不为0的学生

    @Select("SELECT COUNT(id) FROM user WHERE registerStatus=0;")
    Integer getNumberByUserWhereRegisterStatusEqualZero();

    @Update("UPDATE user SET p_st=0,p_st_szpg=0,p_st_xkqz=0,p_st_tyqp=0,p_st_myrx=0,p_st_ldqj=0,p_st_cxcy=0;")
    void updateUserStToZero();//将所有学生的素拓清零

    @Update("UPDATE user SET last_p_st=p_st;")
    void changeUserLast_p_st();//将学生即将被删除的素拓分数保存起来

    @Update("<script>UPDATE user SET " +
                "<if test=\"tag==0\"> p_st_szpg=p_st_szpg+1 </if>"+
                "<if test=\"tag==1\"> p_st_xkqz=p_st_xkqz+1 </if>"+
                "<if test=\"tag==2\"> p_st_tyqp=p_st_tyqp+1 </if>"+
                "<if test=\"tag==3\"> p_st_myrx=p_st_myrx+1 </if>"+
                "<if test=\"tag==4\"> p_st_ldqj=p_st_ldqj+1 </if>"+
                "<if test=\"tag==5\"> p_st_cxcy=p_st_cxcy+1 </if>"+
                "<if test=\"tag==6\"> p_st=p_st+1 </if>" +
            "WHERE id=#{id};</script>")
    void updateStByIdAndTag(@Param("id") Integer id,@Param("tag") Integer tag);//为该学生指定类型素拓加一分

    @Select("SELECT account FROM user;")
    String[] getAllAccount();//获取所有已经注册了的学号

    @Insert("INSERT INTO user (account,password,name,className,punishDate) VALUES (#{account},#{password},#{name}," +
            "#{className},#{punishDate});")
    void register(@Param("account") String account, @Param("password") String password, @Param("name") String name,
                  @Param("className") String className,@Param("punishDate") Date punishDate);//学生注册账号

    @Insert("INSERT INTO user (account,password,name,className,punishDate,registerStatus) VALUES (#{account},#{password},#{name}," +
            "#{className},#{punishDate},1);")
    void registerByAdmin(@Param("account") String account, @Param("password") String password,
                         @Param("name") String name,@Param("className") String className,
                         @Param("punishDate") Date punishDate);//管理员注册账号

    @Update("UPDATE user SET account='已注销' WHERE id=#{id};")
    void deleteUserById(Integer id);//通过id注销学生/拒绝某位学生的注册申请

    @Update("UPDATE user SET name=#{name},className=#{className} WHERE id=#{id};")
    void changeUserInfo(@Param("name") String name,@Param("className") String className,
                        @Param("id") Integer id);//修改学生信息

    @Update("UPDATE user SET punishDate=#{punishDate} WHERE id=#{id};")
    void changePunishDateById(@Param("id") Integer id,@Param("punishDate") Date punishDate);//根据学生id对其进行处罚

    @Select("SELECT * FROM user WHERE registerStatus=0 LIMIT #{start},10;")
    User[] getUserApplies(Integer start);

    @Update("UPDATE user SET registerStatus=1 WHERE id=#{id};")
    void agreeRegister(Integer id);//通过指定id学生的注册申请

    @Delete("DELETE FROM user WHERE id=#{id};")
    void disagreeRegister(Integer id);//拒绝申请的学生

    @Select("SELECT * FROM user WHERE id=#{id} AND password=#{password};")
    User beforeChangePassword(String password,Integer id);//学生修改密码之前进行校验

    @Update("UPDATE user SET password=#{password} WHERE id=#{id};")
    Integer changePassword(String password,Integer id);//学生修改密码
}